package com.tenement.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tenement.entity.Post;
import com.tenement.mapper.PostMapper;
import com.tenement.service.PostService;
import com.tenement.vo.QueryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import static com.tenement.entity.table.PostTableDef.POST;

/**
 *  服务层实现。
 *
 * @author BertXie
 * @since 1.0.1
 */
@Service
@RequiredArgsConstructor
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {

    private final PostMapper postMapper;

    @Override
    public Page<Post> list(QueryVo queryVo) {
        QueryWrapper queryWrapper = QueryWrapper.create().from(POST)
                .where(
                        POST.NAME.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord()))
                                .or(POST.CODE.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(POST.REMARK.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                )
                .and(POST.STATUS.eq(queryVo.getStatus(),queryVo.getStatus() != -1))
                .orderBy(POST.SORT_ORDER,false)
                .orderBy(POST.CREATE_TIME,false);
        return postMapper.paginate(queryVo.getPage(), queryVo.getSize(), queryWrapper);
    }

}
